import { defineConfig, ConfigEnv, loadEnv } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';

import vue from '@vitejs/plugin-vue';

import setupPlugins from './vite/plugins';
import { parseRnv } from './vite/util';
import alias from './vite/alias';

export default ({ command, mode }: ConfigEnv) => {
  // 会在终端打印
  const isBuild = command === 'build';
  const root = process.cwd();
  // 得到.env定义的内容,在终端打印
  const env = parseRnv(loadEnv(mode, root));

  return {
    server: {
      open: '/admin/home'
    },
    build: {
      rollupOptions: {
        emptyOutDir: true,
        output: {
          manualChunks(id: string) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          }
        }
      }
    },
    plugins: [visualizer(), ...setupPlugins(isBuild)],
    resolve: {
      alias
    }
  };
};
